package com.navinfo.parkingnavi.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.navinfo.parkingnavi.Adapter.SearPoiAutoAdapter;
import com.navinfo.parkingnavi.R;
import com.navinfo.parkingnavi.system.SystemConfig;
import com.navinfo.parkingnavi.system.UrlManager;
import com.navinfo.parkingnavi.util.GsonManager;
import com.navinf.parkingnavi.util.MapManager;
import com.navinfo.parkingnavi.util.NetManager;
import com.navinfo.parkingnavi.util.SystemUtil;
import com.navinfo.parkingnavi.util.mode.Poi;
import com.navinfo.parkingnavi.widget.ScrollLayout;
import com.navinfo.parkingnavi.widget.content.ContentRecyclerView;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/*
 *com.navinfo.parkingnavi.fragment
 *zhjch
 *2020/5/18
 *10:47
 *说明（poi抽屉列表）
 */
public class PoiListFragment extends BaseFragment {

    private ScrollLayout mScrollLayout;
    private ContentRecyclerView mListView;
    private String key;
    private Call moreCall;
    private String cityCode = null;
    private View mLoc;
    private Poi mCenterPoi;
    /**
     * poi列表适配器
     */
    private SearPoiAutoAdapter mAdapter;


    private Handler mHandler = new Handler(Looper.getMainLooper());


    /*
     * 让activity监听
     */
    public interface OnPoiListFragmentListener extends OnBaseFragmentListener {

        //显示周边搜索结果
        void onShowNearPoi(Poi centerPoi, Poi nearPoi);

        void onShowRoutePlanning(Poi poi);
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mFragmentView = inflater.inflate(R.layout.fragment_poi_list, null);
        mListView = mFragmentView.findViewById(R.id.poi_list_recycleview);
        mScrollLayout = mFragmentView.findViewById(R.id.poi_list_scrolllayout);
        mLoc = mFragmentView.findViewById(R.id.poi_list_loc);
        mLoc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MapManager.getInstance().jumpCurrentLoc();
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mListView.setLayoutManager(layoutManager);
        mAdapter = new SearPoiAutoAdapter();
        mAdapter.addOnItemClickListener(new SearPoiAutoAdapter.OnItemClickListener() {
            @Override
            public void onShowPoi(Poi poi) {
                if (mListener != null && mListener instanceof OnPoiListFragmentListener) {
                    ((OnPoiListFragmentListener) mListener).onShowNearPoi(mCenterPoi, poi);
                }
            }

            @Override
            public void onShowNavi(Poi poi) {
                if(mListener != null && mListener instanceof OnPoiListFragmentListener){
                    ((OnPoiListFragmentListener) mListener).onShowRoutePlanning(poi);
                }
            }
        });
        mListView.setAdapter(mAdapter);
        mScrollLayout.setMinOffset(SystemUtil.getScreenHeight(getActivity()) / 2 - 200);
        mScrollLayout.setOnScrollChangedListener(new ScrollLayout.OnScrollChangedListener() {
            @Override
            public void onScrollProgressChanged(float currentProgress) {
                if (mLoc.getVisibility() == View.VISIBLE)
                    mLoc.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onScrollFinished(ScrollLayout.Status currentStatus) {
                if (mLoc.getVisibility() != View.VISIBLE)
                    mLoc.setVisibility(View.VISIBLE);
                if (currentStatus == ScrollLayout.Status.CLOSED) {
                    MapManager.getInstance().changeMapPaddingBottom(SystemUtil.getScreenHeight(getActivity()) / 2 - 250);
                } else if (currentStatus == ScrollLayout.Status.OPENED) {
                    MapManager.getInstance().changeMapPaddingBottom(0);
                }
            }

            @Override
            public void onChildScroll(int top) {
            }
        });
        //加载更多数据
        mListView.setMoreListener(new ContentRecyclerView.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                int count = mAdapter.getItemCount();
                if (moreCall == null && !TextUtils.isEmpty(key) && count % 10 == 0) {
                    int page = count / 10 + 1;
                    String url;
                    if (cityCode != null)
                        url = UrlManager.getPoiSearchUrl(key, cityCode, page, 10);
                    else
                        url = UrlManager.getPoiSearchUrl(key, page, 10);
                    moreCall = NetManager.getInstance().getData(url, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            moreCall = null;
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            moreCall = null;
                            final ArrayList<Poi> list = GsonManager.getInstance().parsePoiList(response.body().string());
                            mHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    if (list != null) {
                                        mAdapter.addData(list);
                                    }
                                }
                            });
                        }
                    });
                }
            }
        });
        return mFragmentView;
    }

    @Override
    public void onDataChange(Bundle bundle) {
        if (bundle != null) {
            Serializable serializable = bundle.getSerializable(SystemConfig.Bundle_key_poi_list);
            if (serializable != null && serializable instanceof ArrayList) {
                mAdapter.setData((ArrayList<Poi>) serializable);
            }
            key = bundle.getString(SystemConfig.Bundle_key_search_key);
            cityCode = bundle.getString(SystemConfig.Bundle_key_cityCode);
        }
    }

    @Override
    public void onHideOrRemove(boolean bRemove) {
        super.onHideOrRemove(bRemove);
        MapManager.getInstance().changeMapPaddingBottom(0);
    }
}
